package com.jackrain.clouddata.oms.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.clouddata.oms.dao.OrderBackDao;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/*
   created by tangzhipeng on 2017/08/17
 */
@Slf4j
@RestController
public class OrderBackController {

    @RequestMapping(path = "/api/oms/saleBack", method = RequestMethod.POST)
    public JSONObject saleBack(HttpServletRequest request) {
        log.info("-------------------执行销售入库--------------------------");
        OrderBackDao orderBack = ApplicationContextHandle.getBean(OrderBackDao.class);
        String jo = request.getParameter("DATA");
        JSONObject jsonObject = JSON.parseObject(jo);
        String tag = request.getHeader("TAG");
        String queryTopicId = "SELECT TOPIC FROM OMS_TOPIC WHERE TOPIC='OMS_M_SALE_BACK'";
        JSONObject topicIdJo = QueryEngine.getInstance().doQueryObject(queryTopicId);
        String topic = topicIdJo.getString("TOPIC");
        orderBack.updateSaleOrTranfer(jsonObject, tag, topic);
        ValueHolder valueHolder = new ValueHolder();
        valueHolder.put("code",0);
        return valueHolder.toJSONObject();
    }

    @RequestMapping(path = "/api/oms/transferBack", method = RequestMethod.POST)
    public JSONObject transferBack(HttpServletRequest request) {
        log.info("-------------------执行调拨入库--------------------------");
        OrderBackDao orderBack = ApplicationContextHandle.getBean(OrderBackDao.class);
//        String tag1=request.getParameter("tag");
        String jo = request.getParameter("DATA");
        JSONObject jsonObject = JSON.parseObject(jo);
        String tag = request.getHeader("TAG");
        String queryTopicId = "SELECT TOPIC FROM OMS_TOPIC WHERE TOPIC='OMS_M_TRANSFER_BACK'";
        JSONObject topicIdJo = QueryEngine.getInstance().doQueryObject(queryTopicId);
        String topic = topicIdJo.getString("TOPIC");
        orderBack.updateSaleOrTranfer(jsonObject, tag, topic);
        ValueHolder valueHolder = new ValueHolder();
        valueHolder.put("code",0);
        return valueHolder.toJSONObject();
    }

    @RequestMapping(path = "/api/oms/saleRetApply", method = RequestMethod.POST)
    public JSONObject retsale(HttpServletRequest request) {
        log.info("-------------------执行销售退货插入--------------------------");
        OrderBackDao orderBack = ApplicationContextHandle.getBean(OrderBackDao.class);
        String jo = request.getParameter("DATA");
        JSONObject jsonObject = JSON.parseObject(jo);
        String tag = request.getHeader("TAG");
        String queryTopicId = "SELECT TOPIC FROM OMS_TOPIC WHERE TOPIC='OMS_M_RET_SALE_APPLY_BACK'";
        JSONObject topicIdJo = QueryEngine.getInstance().doQueryObject(queryTopicId);
        String topic = topicIdJo.getString("TOPIC");
        orderBack.insertRetail(jsonObject, tag, topic);
        ValueHolder valueHolder = new ValueHolder();
        valueHolder.put("code",0);
        return valueHolder.toJSONObject();
    }
}
